package com.tgg.arcsoftfaceservice.controller;


import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 田国刚
 * @since 2023-04-16
 */
@RestController
@RequestMapping("/file-info")
public class FilesController {
    @PutMapping("/put")
    Object putFile(@RequestBody Map<String, Object> resout) {


        return null;
    }

    @GetMapping("/get")
    void File(@RequestBody Map<String, Object> resout, HttpServletResponse res) throws IOException {
        String fileid = resout.get("fileid").toString();
        fileid = "C:\\Users\\tiang\\Desktop\\123\\" + fileid;
        File file = new File(fileid);
        FileInputStream inputStream = new FileInputStream(file);
        OutputStream os = null;
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        inputStream.close();
        byte[] data = outStream.toByteArray();
        res.reset();
        res.addHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileid, "UTF-8"));
        res.addHeader("Access-Control-Allow-Origin", "*");
        res.setContentType("application/octet-stream");
        os = res.getOutputStream();
        os.write(data);
        os.flush();
        os.close();

    }
}
